﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.Xml.Linq;

namespace OmnipotentFlashlight
{
    public partial class BackServer : PhoneApplicationPage
    {
        ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };
        string BackServerUrl = "";

        public BackServer()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            Loaded += (_, __) =>
            {
                SystemTray.ProgressIndicator = _progress;
                BackServerUrl = String.Format("http://112.65.245.231/cpic/useradvise.aspx?phonename={0}&phonevs={1}&phonevsno={2}" +
                    "&keyvalue={3}&imei={4}&softvs={5}&softid={6}&flatid={7}&appid={8}&changekey={9}", App.ads.phonename, App.ads.phonevs, App.ads.phonevsno,
                    App.ads.keyvalue, App.ads.imei, App.softvs, App.softid, App.ads.flatid, App.Appid, App.channelkey);
                txt_content.Focus();
            };
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_progress.IsVisible) { return; }
            if (txt_content.Text.ToString().Length == 0) { MessageBox.Show("温馨提示：请填写反馈内容。"); return; }
            if (txt_content.Text.Length >= 100) { MessageBox.Show("温馨提示：请简洁填写，100字以内。"); return; }
            BackServerUrl += "&content=" + HttpUtility.UrlEncode(txt_content.Text.ToString());
            WebClient web = new WebClient();
            web.OpenReadCompleted += (s, ee) =>
            {
                if (ee.Error != null)
                {
                    MessageBox.Show("网络无法连接，请重试！");
                    _progress.IsVisible = false;
                    return;
                }
                try
                {
                    using (Stream streamResult = ee.Result)
                    {
                        XElement root = XElement.Load(streamResult);
                        var tests = from el in root.Elements("resultinfo") select el;
                        foreach (var a in tests)
                        {
                            if (a.Attribute("result").Value == "1")   //成功
                            {
                                MessageBox.Show("反馈成功！");
                                NavigationService.GoBack();
                            }
                            break;
                        }
                    }
                    _progress.IsVisible = false;
                }
                catch (Exception)
                {
                    MessageBox.Show("反馈失败！请稍后再试！");
                    _progress.IsVisible = false;
                }
            };
            _progress.IsVisible = true;
            web.OpenReadAsync(new Uri(BackServerUrl));
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (_progress.IsVisible) { e.Cancel = true; }
            base.OnBackKeyPress(e);
        }
    }
}
